package filesystem;
import java.util.ArrayList;
import java.util.Iterator;
import tabletree.*;


public class FileSystem 
{
	private FileSystemAdapter adapter;
	private Directory root = null;
	private Directory current = null;
	
	static private FileSystem filesystem = null;	
	
	private FileSystem()
	{		
	}
	
	public static FileSystem getInstance(Directory root)
	{
		if (filesystem == null)		
			filesystem = new FileSystem();
		filesystem.setRoot(root);
		filesystem.setCurrent(root);
		
		return filesystem;
	}
	
	public void showAllElements()
	{
		if (root!=null)		
			System.out.println(root.getDetailListing(""));	
	}

	public void setRoot(Directory root) 
	{
		this.root = root;
	}

	public Directory getRoot() 
	{
		return root;
	}

	
	public void setCurrent(Directory current) 
	{
		this.current = current;
	}

	public Directory getCurrent() 
	{
		return current;
	}
	public void addChild(Directory parent, FSElement child, int index)
	{
		parent.addChild(child, index);
	}
	
	public FSElement getChild(Directory parent, int index)
	{
		return parent.getChildren().get(index);
	}
	
	public String getFSElementInfo(FSElement element)
	{
		return element.getInfo(" ");
	}
	
	public ArrayList<String> getCurrentChildrenInfo()
	{
		ArrayList<String> info = new ArrayList<String>();
		Iterator<FSElement> it = current.getChildren().iterator();
		
		while(it.hasNext())
		{
			info.add(it.next().getInfo(" "));
		}
		
		return info;
	}

	public void setAdapter(FileSystemAdapter adapter) 
	{
		this.adapter = adapter;
	}
}
